/*
	The MIT License

	Copyright (c) 2010 IFMO/GameDev Studio

	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE.
*/

#pragma once


/*-----------------------------------------------------------------------------
	ETriMesh
-----------------------------------------------------------------------------*/

class ETriMesh : public ITriMesh {
	public:
								ETriMesh		( uint format );
								~ETriMesh		( void );
						
		//	Basic stuff :
		virtual uint			GetFormat		( void ) const { return format; }
		virtual void			SetFormat		( uint format ) { this->format = format; }
	
		virtual void			SetVertexNum	( uint num );
		virtual uint			GetVertexNum	( void ) const;
		virtual uint			AddVertex		( const EVertex &v );
		virtual void			SetVertex		( uint index, const EVertex &v );
		virtual EVertex			GetVertex		( uint index ) const;
		
		virtual uint			AddUniqueVertex	( const EVertex &v, float tolerance );
		
		virtual void			SetTriangleNum	( uint num );
		virtual uint			GetTriangleNum	( void ) const;
		virtual uint			AddTriangle		( uint i0, uint i1, uint i2 );
		virtual uint			AddTriangle		( const EVertex& v0, const EVertex& v1, const EVertex & v2);
		virtual void			SetTriangle		( uint index, uint i0,  uint i1,  uint i2 );
		virtual void			GetTriangle		( uint index, uint &i0, uint &i1, uint &i2 ) const;

		virtual void			AttachSG		( const EShadingGroup &sg );
		virtual void			DetachSG		( uint index );
		virtual uint			GetSGNum		( void ) const;
		virtual void			SetSGNum		( uint num );
		virtual EShadingGroup	GetSG			( uint index ) const;
		virtual void			SetSG			( uint index, const EShadingGroup &sg );
		virtual void			SetSGToAllTris	( const EString &name );
		
		virtual IPxTriMesh		Clone			( void ) const;
		
		virtual void			WriteXML		( rapidxml::xml_node<> *xnode ) const;
		virtual void			ReadXML			( rapidxml::xml_node<> *xnode );
		
		//	sophistic stuff :
		virtual void			Transform		( const EMatrix &T );
		virtual void			GlueSGs			( void );
		virtual void			MergeVertices	( void );
		virtual void			Split			( const EPlane &plane, IPxTriMesh left, IPxTriMesh right );
		virtual void			ComputeNormals	( void );
		virtual void			ComputeTangents	( void );
		virtual EBBox			ComputeBBox		( void ) const;
		
		virtual EPoint			TriangleCenter	( uint index ) const;
		virtual EVector			TriangleNormal	( uint index ) const;
		virtual float			TriangleArea	( uint index ) const;

		virtual void			Merge			( IPxTriMesh mesh);
		virtual bool			IsEmpty			( void ) const;
		virtual void			Clear			( void );
		virtual bool			RayIntersect	( const EPoint &origin, const EVector &direction, EVertex &result) const;
		virtual bool			RayIntersect	( const EPoint &origin, const EVector &direction, ITriMeshHitCB *report) const;
		virtual bool			GetDecal		( const EPoint &origin, const EVector &direction, float size, IPxTriMesh result, EVertex &v);
		
		//	animation stuff :
		virtual bool			IsSkinned		( void ) const { return !joints.empty(); }
		virtual void			AddJoint		( EJoint joint ) { joints.push_back( joint ); }

		virtual uint			GetJointNum		( void ) const		 { return joints.size(); }
		virtual EJoint			*GetJoint		( uint index )		 { ASSERT(index<joints.size()); return &joints[index]; }
		virtual const EJoint	*GetJoint		( uint index ) const { ASSERT(index<joints.size()); return &joints[index]; }
		virtual uint			GetJointIDByName( const EString name );

		virtual IPxTriMesh		CreateLimb		( EString joint_name, float threshold, bool include_childs );
		
	protected:
		bool					CompareVertices	( const EVertex &v0, const EVertex &v1, float tolerance ) const;

	protected:
		//	Vertex merging stuff :
		struct octree_node_s {
			octree_node_s () {	memset(this, 0, sizeof(octree_node_s));	}
			uint			vert_index;
			octree_node_s	*nodes[8];
		};

		uint		InsertVertex		( const EVertex &v, float tolerance, octree_node_s *&node );
		void		CleanupOctree		( octree_node_s *node );
		uint		ClassifyVertices	( const EVertex &v0, const EVertex &v1 );
		void		DeOptimize			( void );
		
		octree_node_s	*root;
		
		void		GetTriangleV0V1		( uint index, EVector &v0, EVector &v1, EPoint &center ) const;
		
	protected:
		
		struct triangle_s {
			triangle_s() : i0(0), i1(0), i2(0) {}
			triangle_s(uint i0, uint i1, uint i2) : i0(i0), i1(i1), i2(i2) {}
			uint i0, i1, i2;
		};
		uint					format;
		
		std::vector<EVertex>			vertices;
		std::vector<triangle_s>		triangles;
		std::vector<EShadingGroup>	shading_groups;

		//	animation stuff :
		std::vector<EJoint>	joints;
	};

																					   
																					   